<?php
namespace app\common\model;
class UserCollectShop extends Base{

    public function getShopByTag(){
        return $this->belongsToMany("LitestoreTag","LitestoreTagMid","tid","lid");
    }
    public function  myCollectStore(){
        return $this->hasMany("LitestoreShop","id","shop_id")->field('id,name,address,desc,info,longitude,latitude,average,shop_image');
    }
    /**
     * @param $uid
     * @param $id
     * @param $type 1 收藏 2 删除收藏
     * 收藏/取消收藏店铺
     * 1成功 0失败 -1已添加不许重复添加 -2不存在无法删除
     */
    public function collect_shop($uid,$id,$type)
    {
        $data['user_id'] = $uid;
        $data['shop_id'] = $id;

        $info = $this->where($data)->find();

        if($type == 1){

            if($info) return -1;
            $data['createTime'] = time();
            $res = $this->insert($data);
            if($res) return 1;

        }elseif($type == 2){

            if(!$info) return -2;
            $res = $this->where($data)->delete();
            if($res) return 1;

        }


        return 0;

    }

    public function user_collect_shop($uid=0)
    {


        $list = $this->alias('cs')->field('s.*')
            ->where('cs.user_id',$uid)
            ->join('__LITESTORE_SHOP__ s','s.id=cs.shop_id')
            ->order('id desc')->page(input('page/d',1))->limit(10);
        $list = collection($list->with("getShopByTag")->select())->toArray();


        return $list;
    }
}